package eventplanner.ejb.adders;

import eventplanner.entities.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;

@Stateless
public class TagsAdder {

	private static final Logger LOG = LoggerFactory.getLogger(TagsAdder.class);

	@PersistenceContext
	private EntityManager em;

	public List<Event> readWithTags(Set<String> tags) {
		if (tags.isEmpty()) {
			return Collections.emptyList();
		}

		Set<Event> events = new HashSet<>();
		for (String tag : tags) {
			TypedQuery<Event> query = em.createQuery("select distinct e from Event e join e.tags t where t = ?1", Event.class);
			query.setParameter(1, tag);
			events.addAll(query.getResultList());
		}
		return new ArrayList<>(events);
	}
}
